package com.deekian.tools.bluetoothtrigger.utils;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;

import com.deekian.tools.bluetoothtrigger.R;
import com.deekian.tools.bluetoothtrigger.model.WifiHotspotInfo;
import com.deekian.tools.bluetoothtrigger.second.SettingActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Set;

/**
 * Created by deekian on 18-7-5.
 */

public class SharedPreferencesTools {
    public static final String BLUETOOTH_DEVICE_NAME_KEY = "name";
    public static final String BLUETOOTH_DEVICE_ADDRESS_KEY = "address";

    //获取已存储的WIFI热点信息
    public static WifiHotspotInfo getWIFIWifiHotspotInfo(Context context) {
        SharedPreferences sharedPreferences = getSharedPreferences(context);
        return new WifiHotspotInfo(
                sharedPreferences.getString(context.getString(R.string.cache_key_wifi_hotspot_name), context.getString(R.string.wifi_hotspot_def_name)),
                sharedPreferences.getString(context.getString(R.string.cache_key_wifi_hotspot_password), context.getString(R.string.wifi_hotspot_def_password)));
    }

    //更新WIFI热点信息
    public static void updateWifiHotspotInfo(Context context, WifiHotspotInfo info) {
        SharedPreferences.Editor editor = context.getSharedPreferences(context.getString(R.string.sharedpreferences_cache_flag), Context.MODE_PRIVATE).edit();
        editor.putString(context.getString(R.string.cache_key_wifi_hotspot_name), info.getHostname());
        editor.putString(context.getString(R.string.cache_key_wifi_hotspot_password), info.getPassword());
        editor.apply();
    }

    //检查开关状态
    public static boolean testBluetoothBroadcastReceiver(Context context) {
        return getSharedPreferences(context).getBoolean(context.getString(R.string.master_switch), false);
    }

    //关闭开关
    public static void stopBluetoothBroadcastReceiver(Context context) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putBoolean(context.getString(R.string.master_switch), false);
        editor.apply();
    }


    //打开开关
    public static void starBluetoothBroadcastReceiver(Context context) {
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putBoolean(context.getString(R.string.master_switch), true);
        editor.apply();
    }

    //获得系统存储的已配对的设备列表
    public static JSONArray getSystemBluetoothDeviceArray() {
        JSONArray bluetoothArray = new JSONArray();
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> bondedDevices = defaultAdapter.getBondedDevices();

        for (BluetoothDevice bond : bondedDevices) {
            JSONObject device = new JSONObject();
            try {
                device.put(BLUETOOTH_DEVICE_NAME_KEY, bond.getName()).put(BLUETOOTH_DEVICE_ADDRESS_KEY, bond.getAddress());
                bluetoothArray.put(device);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return bluetoothArray;
    }

    //获得已经存储的设备列表
    public static JSONArray getCacheBluetoothDeviceArray(Context context) {
        //获取存储数据对象
        SharedPreferences preferences = getSharedPreferences(context);
        JSONArray selected_device = null;
        try {
            selected_device = new JSONArray(preferences.getString(context.getString(R.string.cache_key_bluetooth_device_list), "[]"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return selected_device;
    }

    //添加新的设备到列表
    public static void addBluetoothDevice(Context context, SettingActivity.SpinnerOption spinnerOption) {
        JSONArray cache = SharedPreferencesTools.getCacheBluetoothDeviceArray(context);
        JSONObject newCache = new JSONObject();
        try {
            newCache.put(BLUETOOTH_DEVICE_NAME_KEY, spinnerOption.getBtName());
            newCache.put(BLUETOOTH_DEVICE_ADDRESS_KEY, spinnerOption.getAddress());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        cache.put(newCache);
        //更新存储内容
        updateBluetoothDevice(context, cache);
    }

    //删除指定的配置
    public static void deleteBluetoothDevice(Context context, int id) {
        JSONArray cache = getCacheBluetoothDeviceArray(context);
        cache.remove(id);
        updateBluetoothDevice(context, cache);
    }

    //更新存储内容
    static void updateBluetoothDevice(Context context, JSONArray deviceArray) {
        //获取存储数据对象
        SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putString(context.getString(R.string.cache_key_bluetooth_device_list), deviceArray.toString());
        editor.apply();
    }

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(context.getString(R.string.sharedpreferences_cache_flag), Context.MODE_PRIVATE);
    }

    private static SharedPreferences.Editor getSharedPreferencesEditor(Context context) {
        return getSharedPreferences(context).edit();
    }
}
